package ru.ssau.berlin.ejb.facade;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import ru.ssau.berlin.entity.AttributeType;
import ru.ssau.berlin.entity.PersonRole;

/**
 *
 * @author Пользователь
 */
@Stateless
public class UniFacade implements UniFacadeLocal {

    @PersistenceContext
    EntityManager em;

    public List<AttributeType> findAttributeTypes() {
        return em.createNamedQuery("findAllAttributeTypes").getResultList();
    }

    public AttributeType findAttributeType(long id) {
        return em.find(AttributeType.class, id);
    }

    public AttributeType findAttributeType(String name) {
        try {
            return (AttributeType)em.createNamedQuery("findAttributeTypeByName")
                    .setParameter("typeName", name).getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }

    public void createAttributeType(AttributeType type) {
        em.persist(type);
    }

    public void createRole(PersonRole role) {
        em.persist(role);
    }

    public void editRole(PersonRole role) {
        em.merge(role);
    }
    
    public void removeRole(PersonRole role) {
        em.remove(em.merge(role));
    }

    public PersonRole findRole(long id) {
        return em.find(PersonRole.class, id);
    }

}
